// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.database.editors;

import java.io.ByteArrayInputStream;

import org.dengues.core.CorePlugin;
import org.dengues.core.IWarehousePluginService;
import org.dengues.core.editor.AbstractEditorInput;
import org.dengues.core.resource.DenguesResourceManager;
import org.dengues.core.sql.SqlUtils;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.core.warehouse.IWarehouseNode;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.model.database.EMFDataType;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;

/**
 * qzhang class global comment. Detailled comment <br/>
 * 
 */
public class DBFileEditorInput extends AbstractEditorInput {

    private IFile sqlScriptFile;

    private final DatabaseDiagram dbDiagram;

    public DBFileEditorInput(DatabaseDiagram dbDiagram, IPath path) {
        super(dbDiagram.getName(), ENodeCategoryName.DATABASE, path);
        this.dbDiagram = dbDiagram;
    }

    /**
     * Sets the warehouseNode.
     * 
     * @param warehouseNode the warehouseNode to set
     */
    public void setWarehouseNode(IWarehouseNode warehouseNode) {
        if (warehouseNode != null) {
            this.warehouseNode = warehouseNode;
        } else {
            IWarehousePluginService warehouseService = CorePlugin.getDefault().getWarehousePluginService();
            this.warehouseNode = warehouseService.getWarehouseViewFactory().createWarehouseNodeFromEMF(
                    ENodeCategoryName.DATABASE, parentNode, dbDiagram);
        }
    }

    public IFile getSqlScriptFile() {
        sqlScriptFile = DenguesResourceManager.getSqlScriptFile(dbDiagram.getName());
        String sqlStatement = getSqlStatement();
        dbDiagram.setWholeSql(sqlStatement);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(sqlStatement.getBytes());
        try {
            if (!sqlScriptFile.exists()) {
                sqlScriptFile.create(inputStream, true, null);
            } else {
                sqlScriptFile.setContents(inputStream, true, false, null);
            }
            sqlScriptFile.getParent().refreshLocal(IResource.DEPTH_INFINITE, null);
        } catch (CoreException e) {
            e.printStackTrace();
        }
        return sqlScriptFile;
    }

    public DatabaseDiagram getDbDiagram() {
        return this.dbDiagram;
    }

    public String getSqlStatement() {
        String sql = "";
        int type = dbDiagram.getEmfType().getValue();
        switch (type) {
        case EMFDataType.DATABASE:
            sql = SqlUtils.getSqlStatement(dbDiagram);
            break;
        case EMFDataType.QUERY:
            sql = SqlUtils.getSelectStatement(dbDiagram);
            break;
        default:
            break;
        }
        return sql;
    }
}
